package com.zthz.douxing.sendmessageservice.handlers

import com.zthz.douxing.event.UserWillDepartEvent
import com.zthz.douxing.event.UserWillTripJob
import com.zthz.douxing.msg.MessageType
import com.zthz.douxing.sendmessageservice.thirdmessagesender.BaseMessageSenderService
import com.zthz.douxing.sendmessageservice.utils.OrderXingeMessageService
import com.zthz.douxing.utils.CommonUtil
import groovy.time.TimeCategory
import org.quartz.JobDataMap
import org.quartz.JobKey
import org.quartz.SimpleTrigger
import org.quartz.impl.triggers.SimpleTriggerImpl

import static org.quartz.JobBuilder.newJob

class PlanTripMessageBefore3HoursHanlderService extends AbstractMessageHandlerService{


    @Override
    protected void handle(JobDataMap jobDataMap) {

        Date now = new Date()
        UserWillDepartEvent event =  jobDataMap.get("userWillTripEvent")
        if(!event?.getuiId && !event?.xgToken){
            return
        }
        Date isLess3Minutes
        use(TimeCategory){
            isLess3Minutes = CommonUtil.getFromDuration2Date(
                    TimeCategory.minus(event.tripSegment.startTime,(now+3.minutes)))
        }
        if(event.tripSegment.startTime.getTime() - now.getTime()>0 && isLess3Minutes.getTime()>0){

            Date trigDateTime
            Date isLess3HouseFromNow
            use(TimeCategory) {
                isLess3HouseFromNow =
                        isLess3HouseFromNow = CommonUtil.getFromDuration2Date(
                              TimeCategory.minus(event.tripSegment.startTime,(now+3.hours))
                        )
            }

            if(isLess3HouseFromNow?.getTime()<=0){
                use(TimeCategory) {
                    trigDateTime = now+3.minutes
                }

            }else{
                use(TimeCategory) {
                    trigDateTime = event.tripSegment.startTime - 3.hours
                }
            }
            def before3HourseClosure = {JobDataMap jobmap->
                try{
                    BaseMessageSenderService baseMessageSenderService = jobmap.get("baseMessageSenderService")
                    OrderXingeMessageService orderXingeMessageService = jobmap.get("orderXingeMessageService")
                    UserWillDepartEvent userWillTripEvent = jobmap.get("userWillTripEvent")

                    String msgContent = orderXingeMessageService
                            .orderMessage(userWillTripEvent)
                    if(!msgContent){
                        return
                    }
                    Map param = baseMessageSenderService.getParamFromUserWillDepartEvent4PlanForm(userWillTripEvent,msgContent,
                            MessageType.PlanInfo)
                    String resultDesc = baseMessageSenderService.send2One(param)
                    orderXingeMessageService.saveMessageToUserLog(userWillTripEvent,msgContent,resultDesc,MessageType.PlanInfo)
                }catch(Exception e){
                    log.debug(e.getMessage(),e.printStackTrace())
                }finally{
                }
            }

            jobDataMap.put(JOBCONTENT_KEY,before3HourseClosure)
            if(event?.tripSegment?.plantrip?.personOwner?.id){
                String before3HourseJobName = CommonUtil.getJobKey("before3HourseMessage",event?.tripSegment?.plantrip?.personOwner?.id)
                String before3HourseTriggerName =  CommonUtil.getJobKey("planTrip3HourseTrigger",trigDateTime.format("yyyyMMddHHmm"))
                def jobKey = new JobKey(before3HourseJobName,"UserWillTrip")
                def job = newJob(UserWillTripJob).withIdentity(jobKey).usingJobData(jobDataMap).build()
                SimpleTrigger trigger =
                        new SimpleTriggerImpl(before3HourseTriggerName,trigDateTime, null, 0, 0L)

                quartzScheduler.deleteJob(jobKey)
                addJob(job,trigger)
            }

        }

    }

}